package org.uoss.webapp.framework.context;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.web.context.support.XmlWebApplicationContext;
import org.uoss.core.exception.UOSSException;
import org.uoss.core.logging.ILogger;
import org.uoss.core.logging.ILoggerFactory;


public class WebappXmlApplicationContext extends XmlWebApplicationContext {
	private static final ILogger logger = ILoggerFactory.getLogger(WebappXmlApplicationContext.class);
	private static final WebappBeansContext beansContext = WebappBeansContext.getContextInstance();
	
	public WebappXmlApplicationContext() throws UOSSException{
		logger.info(WebappXmlApplicationContext.class, "WebAppBeanContext()", "Loading bean context configuration locations.");
		String packageNames = beansContext.getServletContext().getInitParameter("PROJECT_PACKAGE_NAMES");
		if(packageNames!=null){
			beansContext.setClassPackageNames(packageNames.split(","));
		}
		beansContext.initialApplicationConfig();
	}

	@Override
	protected String[] getDefaultConfigLocations() {
		return new String[0];
	}

	@Override
	public String[] getConfigLocations() {
		String[] defaultConfigLocations = super.getConfigLocations();
		String[] beansConfigLocations = beansContext.getBeansConfigLocations();
		String[] actionConfigLocations = beansContext.getActionConfigLocations();
		List<String> configLocationList = new ArrayList<String>();
		configLocationList.addAll(Arrays.asList(defaultConfigLocations));
		configLocationList.addAll(Arrays.asList(beansConfigLocations));
		configLocationList.addAll(Arrays.asList(actionConfigLocations));
		List<String> locations = new ArrayList<String>(configLocationList.size());
		for (String location : configLocationList) {
			System.out.println("classpath:" + location);
			locations.add("classpath:" + location);
		}
		return locations.toArray(new String[configLocationList.size()]);
	}
}
